<template>
  <div class="login-container">
    <div class="aa">
      <el-form
        ref="formRef"
        :model="loginForm"
        :rules="loginRules"
        class="login-form"
        auto-complete="on"
        label-position="left"
      >
        <div class="title-container">
          <h3 class="title">YKL-Admin</h3>
        </div>
        <el-form-item prop="username">
          <span class="svg-container">
            <svg-icon name="ele-UserFilled" />
          </span>
          <el-input
            ref="username"
            v-model="loginForm.username"
            placeholder="Username"
            name="username"
            type="text"
            tabindex="1"
            auto-complete="on"
          />
        </el-form-item>
        <el-form-item prop="password">
          <span class="svg-container">
            <svg-icon name="ele-Lock" />
          </span>
          <el-input
            :key="passwordType"
            ref="passwordRef"
            v-model="loginForm.password"
            :type="passwordType"
            placeholder="Password"
            name="password"
            tabindex="2"
            auto-complete="on"
            @keyup.enter.native="handleLogin"
          />
          <span class="show-pwd" @click="showPwd">
            <svg-icon :name="passwordType === 'password' ? 'ele-Hide' : 'ele-View'" />
          </span>
        </el-form-item>
        <div class="button">
          <el-button
            :loading="loading"
            type="primary"
            style="width: 50%; margin-bottom: 30px; height: 40px"
            @click.native.prevent="handleLogin"
          >
            登 陆
          </el-button>
          <el-button
            :zhuCeloading="loading"
            type="primary"
            style="width: 50%; margin-bottom: 30px; height: 40px"
            @click.native.prevent="zhuCe"
          >
            注 册
          </el-button>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script lang="ts" setup name="Login">
import type { FormInstance } from 'element-plus'
import { nextTick, ref, watch } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import useStore from '@/stores'
import { ElMessage } from 'element-plus'
import { reqLogin, reqZhuCe } from '@/api/login/loginApi'
import { setToken } from '@/utils/token-utils'
const { UserInfo } = useStore()
/* 
之前的仓库没有做封装
import { useUserInfoStore } from '@/stores/modules/userInfo'
const userInfoStore = useUserInfoStore() */
const route = useRoute()
const router = useRouter()
const loginForm = ref({
  username: 'admin',
  password: '111111'
})
const loading = ref(false)
const passwordType = ref('password')
const redirect = ref('')
const passwordRef = ref<HTMLInputElement>()
const formRef = ref<FormInstance>()
const reload = inject('reload') as any

const validateUsername = (rule: any, value: any, callback: any) => {
  if (value.length < 4) {
    callback(new Error('用户名长度不能小于4位'))
  } else {
    callback()
  }
}
const validatePassword = (rule: any, value: any, callback: any) => {
  if (value.length < 6) {
    callback(new Error('密码长度不能小于6位'))
  } else {
    callback()
  }
}

const loginRules = {
  username: [{ required: true, validator: validateUsername }],
  password: [{ required: true, trigger: 'blur', validator: validatePassword }]
}

watch(
  route,
  () => {
    redirect.value = route.query && (route.query.redirect as string)
  },
  { immediate: true }
)

//切换密码的显示/隐藏
const showPwd = () => {
  if (passwordType.value === 'password') {
    passwordType.value = 'text'
  } else {
    passwordType.value = 'password'
  }
  nextTick(() => {
    passwordRef.value?.focus()
  })
}

//点击登陆的回调
const handleLogin = async () => {
  await formRef.value?.validate()
  loading.value = true
  const { username, password } = loginForm.value
  try {
    await UserInfo.login(username, password)
    router.push({ path: redirect.value || '/' })
    ElMessage.success({ message: `登陆成功` })
  } finally {
    loading.value = false
  }
}

// 注册
const zhuCeloading = ref(false)
const zhuCe = async () => {
  await formRef.value?.validate()
  zhuCeloading.value = true
  const { username, password } = loginForm.value
  try {
    const res = await reqZhuCe(username, password)
    console.log('🚚...  注册', res)
    if (res.code === 0) {
      ElMessage.success({ message: `注册成功` })
    }
  } finally {
    zhuCeloading.value = false
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}
/* reset element-ui css */
.login-container {
  // 登陆背景图
  background-image: url(@/assets/1.png);
  background-color: #b8e5f8 !important;
  background-size: cover;
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;
    .el-input__wrapper {
      width: 100%;
      background-color: transparent;
      box-shadow: none;
      input {
        background: transparent;
        border: 0px;
        // -webkit-appearance: none;
        border-radius: 0px;
        padding: 12px 5px 12px 15px;
        color: $light_gray;
        height: 47px;
        caret-color: $cursor;

        &:-webkit-autofill {
          box-shadow: 0 0 0px 1000px $bg inset !important;
          -webkit-text-fill-color: $cursor !important;
        }
      }
    }
  }
  // 登陆对话框
  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.2);
    border-radius: 5px;
    color: #454545;
  }
  .aa {
    margin: 0px auto;
    margin-top: 160px;
    padding: 10px;
    width: 600px;
    height: 300px;
    // background-color: blue;
    background: rgba(255, 255, 255, 0.2);
    -webkit-backdrop-filter: blur(8px);
    backdrop-filter: blur(8px);
    border-radius: 25px;
    box-shadow: inset 0 0 6px rgba(255, 255, 255, 0.597);
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;
$cursorBai: #fff;
.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;

  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 20px;
    margin: 0 auto;
    overflow: hidden;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    // color: $dark_gray;
    color: #fff;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: #5ab9f8;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 10px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
    color: #fff;
  }
  .button {
    display: flex;
    width: 100%;
  }
}
</style>
